﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace JavaCodeGenerator
{
    class ConfigUtil
    {
        public static Config getConfig()
        {

            Config config = new Config();

            XElement root = XElement.Load(Environment.CurrentDirectory + "/settings.xml");


            config.referModelName = root.Attribute("referModelName").Value;
            config.referModelPackage = root.Attribute("referModelPackage").Value;

            XElement modelEle = root.Elements().Where(t => t.Attribute("name").Value == "model_package").First();
            config.ModelPackagePath = modelEle.Value;

            XElement mapperEle = root.Elements().Where(t => t.Attribute("name").Value == "mapper_package").First();
            config.MapperPackagePath = mapperEle.Value;

            XElement daoEle = root.Elements().Where(t => t.Attribute("name").Value == "dao_package").First();
            config.DaoPackagePath = daoEle.Value;
            config.DaoFileSuffix = daoEle.Attribute("suffix").Value;

            XElement daoImplEle = root.Elements().Where(t => t.Attribute("name").Value == "daoImpl_package").First();
            config.DaoImplPackagePath = daoImplEle.Value;
            config.DaoImplFileSuffix = daoImplEle.Attribute("suffix").Value;

            XElement serviceEle = root.Elements().Where(t => t.Attribute("name").Value == "service_package").First();
            config.ServicePackagePath = serviceEle.Value;
            config.ServiceFileSuffix = serviceEle.Attribute("suffix").Value;

            XElement serviceImplEle = root.Elements().Where(t => t.Attribute("name").Value == "serviceImpl_package").First();
            config.ServiceImplPackagePath = serviceImplEle.Value;
            config.ServiceImplFileSuffix = serviceImplEle.Attribute("suffix").Value;

            return config;
        }

        public static void setConfig(Config config)
        {

            String path = Environment.CurrentDirectory + "/settings.xml";
            XElement root = XElement.Load(path);


            root.Attribute("referModelName").Value = config.referModelName;
            root.Attribute("referModelPackage").Value = config.referModelPackage;

            XElement modelEle = root.Elements().Where(t => t.Attribute("name").Value == "model_package").First();
            modelEle.Value = config.ModelPackagePath;

            XElement mapperEle = root.Elements().Where(t => t.Attribute("name").Value == "mapper_package").First();
            mapperEle.Value = config.MapperPackagePath;

            XElement daoEle = root.Elements().Where(t => t.Attribute("name").Value == "dao_package").First();
            daoEle.Value = config.DaoPackagePath;

            XElement daoImplEle = root.Elements().Where(t => t.Attribute("name").Value == "daoImpl_package").First();
            daoImplEle.Value = config.DaoImplPackagePath;

            XElement serviceEle = root.Elements().Where(t => t.Attribute("name").Value == "service_package").First();
            serviceEle.Value = config.ServicePackagePath;

            XElement serviceImplEle = root.Elements().Where(t => t.Attribute("name").Value == "serviceImpl_package").First();
            serviceImplEle.Value = config.ServiceImplPackagePath;
            root.Save(path);
        }
    }
}
